package Task28;

import java.util.ArrayList;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.DefaultListModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JScrollPane;
import javax.swing.JList;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class ListPanel extends JPanel implements PanelInterface {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField txtC;
	private JPanel panel;
	private JList list;

	public ListPanel() {
		setBorder(null);

		panel = new JPanel();

		txtC = new JTextField();
		txtC.setEditable(false);
		txtC.setText("C:\\");
		txtC.setColumns(10);

		JScrollPane scrollPane = new JScrollPane();
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout
				.setHorizontalGroup(groupLayout
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								Alignment.TRAILING,
								groupLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												groupLayout
														.createParallelGroup(
																Alignment.TRAILING)
														.addComponent(
																scrollPane,
																Alignment.LEADING,
																GroupLayout.DEFAULT_SIZE,
																428,
																Short.MAX_VALUE)
														.addComponent(
																txtC,
																Alignment.LEADING,
																GroupLayout.DEFAULT_SIZE,
																428,
																Short.MAX_VALUE)
														.addComponent(
																panel,
																Alignment.LEADING,
																GroupLayout.DEFAULT_SIZE,
																428,
																Short.MAX_VALUE))
										.addContainerGap()));
		groupLayout.setVerticalGroup(groupLayout.createParallelGroup(
				Alignment.LEADING).addGroup(
				groupLayout
						.createSequentialGroup()
						.addComponent(panel, GroupLayout.PREFERRED_SIZE, 32,
								GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(txtC, GroupLayout.PREFERRED_SIZE,
								GroupLayout.DEFAULT_SIZE,
								GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE,
								223, Short.MAX_VALUE).addContainerGap()));

		initializeRootButtons();

		list = new JList();
		list.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					actionCd();
					actionDir();
				}
				processMouseEvent(e);
			}
		});
		list.setBorder(new LineBorder(new Color(0, 0, 0)));
		scrollPane.setViewportView(list);
		setLayout(groupLayout);

	}

	@Override
	public String getWorkDir() {
		return txtC.getText();
	}

	@Override
	public void setWorkDir(String myWorkDir) {
		txtC.setText(myWorkDir);
	}

	@Override
	public void setData(ArrayList<String> myData) {
		DefaultListModel myModel = new DefaultListModel();
		if (!workDirIsRoot())
			myModel.addElement("..");
		for (int i = 0; i < myData.size(); i++) {
			myModel.addElement(myData.get(i));
		}
		list.setModel(myModel);
	}

	@Override
	public String getSelectValue() {
		Object selected = list.getSelectedValue();
		return (String) selected;
	}

	private void initializeRootButtons() {
		File[] arrayRoots = File.listRoots();
		JButton[] btnRoot = new JButton[arrayRoots.length];

		for (int i = 0; i < arrayRoots.length; i++) {
			btnRoot[i] = new JButton(arrayRoots[i].getPath());
			final String rootDir = arrayRoots[i].getPath();

			btnRoot[i].addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					setWorkDir(rootDir);
					actionDir();
				}
			});

			panel.add(btnRoot[i]);
		}
	}

	private void actionDir() {
		Command command = new Command();
		command = createCommand("dir", "", this.getWorkDir());

		Controller myController = new Controller();
		Result result = new Result();
		result = myController.getResult(command);
		displayResultDir(result);
	}

	private void actionCd() {
		Command command = new Command();
		command = createCommand("cd", this.getSelectValue(), this.getWorkDir());

		Controller myController = new Controller();
		Result result = new Result();
		result = myController.getResult(command);
		if (result.getMessage().contains("xml")) {
			displayResultXML(result);
		} else
			displayResultCd(result);
	}

	private Command createCommand(String sCommand, String arg1, String workDir) {
		Command command = new Command();
		command.setNameCommand(sCommand);
		command.setWorkDir(workDir);
		String[] args = { arg1 };
		command.setArgs(args);
		return command;
	}

	private void displayResultDir(Result result) {
		if (result.getErrors().isEmpty()) {
			if (result.getExceptions().isEmpty()) {
				this.setData(result.getData());
			} else
				msgBox(result.getExceptions());
		} else
			msgBox(result.getErrors());
	}

	private void displayResultCd(Result result) {
		if (result.getErrors().isEmpty()) {
			if (result.getExceptions().isEmpty()) {
				this.setWorkDir(result.getData().get(0));
			} else
				msgBox(result.getExceptions());
		} else
			msgBox(result.getErrors());
	}

	private void msgBox(ArrayList<String> message) {
		String sMessage = "";
		for (int i = 0; i < message.size(); i++)
			sMessage += message.get(i) + "\n";

		JOptionPane.showMessageDialog(null, sMessage, "Fresh Commander",
				JOptionPane.INFORMATION_MESSAGE);
	}

	@Override
	public void refresh() {
		actionDir();
	}

	private boolean workDirIsRoot() {
		boolean result = false;
		String workDir = txtC.getText();
		File[] roots = File.listRoots();
		for (int i = 0; i < roots.length; i++) {
			if (roots[i].getPath().equalsIgnoreCase(workDir))
				result = true;
		}
		return result;
	}

	private void displayResultXML(Result result) {
		if (result.getErrors().isEmpty()) {
			if (result.getExceptions().isEmpty()) {
				ShowWindow myWindow = new ShowWindow();
				myWindow.setText(result.getData());
				myWindow.showWindow();
			} else
				msgBox(result.getExceptions());
		} else
			msgBox(result.getErrors());
	}
}
